﻿public class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    /// <summary>
    /// 迭代法
    /// </summary>
    /// <param name="root"></param>
    /// <param name="targetSum"></param>
    /// <returns></returns>
    public bool HasPathSum(TreeNode root, int targetSum) 
    {
        // 先序遍历
        Stack<KeyValuePair<TreeNode, int>> stack = new Stack<KeyValuePair<TreeNode, int>>();
        int sum = 0;
        if (root is not null)
        {
            sum += root.val;
            stack.Push(new KeyValuePair<TreeNode, int>(root, sum));
        }

        KeyValuePair<TreeNode, int> pair;
        TreeNode node;
        while (stack.Count > 0)
        {
            pair = stack.Pop();
            node = pair.Key;
            sum = pair.Value;

            if (node.left is null && node.right is null && targetSum == sum) return true;
            if (node.right is not null) stack.Push(new KeyValuePair<TreeNode, int>(node.right, sum + node.right.val));
            if (node.left is not null) stack.Push(new KeyValuePair<TreeNode, int>(node.left, sum + node.left.val));
        }

        return false;
    }
    /// <summary>
    /// 递归法
    /// </summary>
    /// <param name="root"></param>
    /// <param name="targetSum"></param>
    /// <returns></returns>
    public bool HasPathSum2(TreeNode root, int targetSum)
    {
        if (root is null) return false;
        
        // 找到路径
        if (root.left is null && root.right is null && targetSum == root.val) return true;

        bool res;
        if (root.left is not null)
        {
            res = HasPathSum2(root.left, targetSum - root.val);
            if (res) return true;
        }
        if (root.right is not null)
        {
            res = HasPathSum2(root.right, targetSum - root.val);
            if (res) return true;
        }

        return false;
    }

    public static void Main(string[] args)
    {
        int?[] root = root = [5, 4, 8, 11, null, 13, 4, 7, 2, null, null, null, 1];
        int targetSum = 22;
        TreeNode rootNode = CreateBinaryTree(root);
        Solution solution = new Solution();
        bool res = solution.HasPathSum2(rootNode, targetSum);
        Console.WriteLine(res);
    }

    private static TreeNode CreateBinaryTree(int?[] root)
    {
        if (root is null || root.Length == 0 || root[0] is null)
        {
            return null;
        }

        Queue<TreeNode> queue = new Queue<TreeNode>();
        TreeNode rootNode = new TreeNode(root[0].Value);
        queue.Enqueue(rootNode);
        int i = 1;
        TreeNode node;

        while (i < root.Length && queue.Count > 0)
        {
            node = queue.Dequeue();
            if (root[i] is not null)
            {
                node.left = new TreeNode(root[i].Value);
                queue.Enqueue(node.left);
            }

            i++;
            if (i >= root.Length) break;
            if (root[i] is not null)
            {
                node.right = new TreeNode(root[i].Value);
                queue.Enqueue(node.right);
            }

            i++;
        }

        return rootNode;
    }
}